#include <drivers/uart.h>
#include <drivers/irq.h>
#include <kernel/initcall.h>
#include <stm32f10x.h>


static const struct gpio_conf uart1[] = {
    { .pin = IO_PA9, .mux = 0xB, .drv = 0x0, .dir = IO_INVALID_DIR, .val = 0, },
    { .pin = IO_PA10, .mux = 0X8, .drv = 0x0, .dir = IO_INVALID_DIR, .val = 1, }
};


void USART1_IRQHandler(void)
{
	while((USART1->SR&(1<<5))==0x20)
	    USART1->DR = USART1->DR;
}


struct uart_device console = {
    .compatible = "stm32f10x-uart",
    .io_base = (void*)USART1_BASE,
    .baudrate = 115200,
    .buf_size = 32,
    .irq_num = USART1_IRQNUM,
    .callback = USART1_IRQHandler,
};


int stm32f10x_stdio_init(void)
{
    int ret = gpio_mux_func(uart1, ARRAY_SIZE(uart1));
    if(ret) {
        return -1;
    }

    uart_device_register(&console);
    irq_enbale(USART1_IRQNUM);
    syslog_bind_uart_device(&console);
    
    return 0;
}


base_initcall(stm32f10x_stdio_init, 2);
